﻿using BookStores.DTO.Response;
using BookStores.IDomain;
using Dapper.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStores.Domain
{
    public class BookAuthors : IBookAuthors
    {
        BookStores.Contracts.IBookAuthorsRepository m_IBookAuthorsRepository;
        public BookAuthors(BookStores.Contracts.IBookAuthorsRepository bookAuthorsRepository)
        {
            m_IBookAuthorsRepository = bookAuthorsRepository;
        }
        /// <summary>
        /// 获取作者列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<(IEnumerable<BookAuthor>,int)> GetBookAuthors(string name , int pageIndex =1,int pageSize=10)
        {
            return await m_IBookAuthorsRepository.GetBookAuthors(name, pageIndex, pageSize);
        }
        /// <summary>
        /// 保存作者
        /// </summary>
        /// <param name="bookAuthors"></param>
        /// <returns></returns>
        public async Task<bool> Insert(Entity.BookAuthors bookAuthors)
        {
            bookAuthors.ID = bookAuthors.NewKey;
            return await m_IBookAuthorsRepository.Insertable(bookAuthors).ExecuteCommandAsync() > 0;
        }
        public async Task<bool> Update(Entity.BookAuthors bookAuthors)
        {
            bookAuthors.ClearPKChanges();
            return await m_IBookAuthorsRepository.Updateable(bookAuthors).ExecuteCommandAsync() > 0;
        }

        public async Task<bool> Delete (string id)
        {
            return await m_IBookAuthorsRepository.Deleteable(id).ExecuteCommandAsync() > 0;
        }
    }
}
